


*-----------------------------------------------------------
* Code type of vote intention (party or undecided?)
*-----------------------------------------------------------

label define wabstype 1 "party" 8888 "don't know/won't vote"
 
forvalues x=$all {
	gen 			wabstype`x' = NIwahl`x'
	recode 			wabstype`x' (1/801=1) (8888 9999=8888)
	label values	wabstype`x' wabstype
}

sum wabstype*

*-------------------------------------
* Vote switching
*--------------------------------------


* Helper variable, all categories that are not parties = missing
foreach num of numlist $all {
	gen wabshelp`num' 	=  NIwahl`num' if wabstype`num'==1
}


egen NIwahl_count 		= rownonmiss(NIwahl?)
gen samplestrict2017 	= NIwahl_count > 2	& !missing(NIwahl9)		//Waverer can only actualize with more than 2 waves




* switching 
egen 	lswitch2017 	= rowsd(NIwahl?)	if samplestrict2017 == 1

replace lswitch2017		= 1 				if lswitch2017>0 & lswitch2017 !=.

list NIwahl? lswitch2017 if samplestrict==1 in 1/500, compress




* Party switching
egen help 				= rowsd(wabshelp*) 	if samplestrict2017==1

gen lpswitch2017 		= help		
replace lpswitch2017 	= 0 				if help==. & samplestrict2017==1
replace lpswitch2017 	= 1 				if lpswitch2017>0 & lpswitch2017!=.




* Waverer
*We need to compare first to last voting behavior
*Therefore we need to compute a variable containing first voting behavior
*Last voting behavior is always NIwahl9 bc of sample restriction
generate 	first_vote	= NIwahl5	if !missing(NIwahl5)
replace		first_vote	= NIwahl6	if !missing(NIwahl6)	& missing(first_vote)
replace		first_vote	= NIwahl7	if !missing(NIwahl7) 	& missing(first_vote)

gen 		comeback	=	1 if wabshelp9 ==	first_vote	&	!missing(wabshelp9) 	//Use wabshelp because it only applies to party voters (non abstainers oä)

* crystallizer

generate crystallizer	=	(first_vote == 8888  | first_vote == 9999) & !missing(wabshelp9) //Initial vote choice: Undecided & voted for a party in the end




*----------------------------------------------------
* Lazarsfeld types
*-----------------------------------------------------

//Watchout: This includes a few voters (switcher) who abstained in the end
lab define laz 			///
	1 "partisan" 		///
	2 "party switcher" ///
	3 "crystallizers"	///
	4 "waverers"		///	came back to initial vote choice after changing minds
	 , replace 
	
gen laztype2017 		= 1 if samplestrict2017==1 & lswitch2017==0 & wabstype5==1
replace	laztype2017		= 2	if samplestrict2017==1 & lswitch2017==1 & lpswitch2017==1
replace	laztype2017		= 3	if samplestrict2017==1 & crystallizer== 1
replace laztype2017 	= 4 if samplestrict2017==1 & lswitch2017==1 & comeback == 1


lab val laztyp* laz

list NIwahl? laztype2017 crystallizer	lswitch2017	lpswitch2017 if samplestrict==1 in 1/500, compress



*----------------------------------------------------
* Lazarsfeld types with respect to SPD Vote
*-----------------------------------------------------

clonevar 	laztype2017_spd = laztype2017
replace 	laztype2017_spd = . if NIwahl9 != 4
replace		laztype2017_spd	= 0 if NIwahl9 != 4 & !missing(NIwahl9)

gen laztype_unstable_spd	= 1 if inlist(laztype2017_spd, 2, 3, 4)

*----------------------------------------------------
* Lazarsfeld types with respect to CDU Vote
*-----------------------------------------------------

clonevar 	laztype2017_cdu = laztype2017
replace 	laztype2017_cdu = . if NIwahl9 != 1
replace		laztype2017_cdu	= 0 if NIwahl9 != 1 & !missing(NIwahl9)



